//
//  HWPromoteViewController.m
//  HWPromoteProjectAPP
//
//  Created by 陈道斌 on 2020/1/20.
//  Copyright © 2020 陈道斌. All rights reserved.
//

#import "HWPromoteViewController.h"
#import "HWPromoteTableViewCell.h"
#import "HWApplyjiesuanViewController.h"
#import "HWDividedIntoViewController.h" // 分成提现
#import "HWPromotionQrCodeViewController.h" //推广二维码
#import "HWPromotionAnnouncementViewController.h" // 推广公告
#import "HWDiscountDaiChunViewController.h" //  折扣代充
#import "HWPromoteGameViewController.h" // 推广游戏







@interface HWPromoteViewController ()<UITableViewDataSource,UITableViewDelegate,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,UICollectionViewDataSource>
@property(strong,nonatomic) UITableView *mainTabelView;
@property(strong,nonatomic) UIView *hederView;

// 卡片Uiview
@property(strong,nonatomic) UIView *kapianView;
@property(strong,nonatomic) UILabel *zeongjingerLabel; //总金额
@property(strong,nonatomic) UILabel *fengxiangyongjingLabel; // 分享金额
@property(strong,nonatomic) UILabel *yitixianjingLabel; //已提现金额
@property(strong,nonatomic) UIButton *shengqingjiesuanBut; // 申请结算


//推广APP的二维码 ,推广公告 ，分成明细
@property(strong,nonatomic)UIView *tuiguanView;







@property(strong,nonatomic) UIView *navigationTopView;
@property(strong,nonatomic) UILabel *zhanghaoLabel;
@property(strong,nonatomic) UILabel *qudaoLabel; //渠道ID
@property(strong,nonatomic) UIImageView *icoIamgeView; // 头像


//请求回来的数据
@property(strong,nonatomic) NSDictionary *dataDic;
@property(assign,nonatomic) NSInteger  hanshuInter;





@end

@implementation HWPromoteViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self sendNetworkRequest];
    [self.navigationController setNavigationBarHidden:YES ];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO];
 
}

- (UITableView *)mainTabelView {
    
    if (_mainTabelView == nil) {
        _mainTabelView = [[UITableView alloc]initWithFrame:CGRectMake(0, NavBarHeight+20, SCREEN_WIDTH, SCREEN_HEIGHT-NavBarHeight-20) style:UITableViewStylePlain];
        _mainTabelView.delegate = self;
        _mainTabelView.dataSource = self;
        _mainTabelView.backgroundColor = beijingyanSe;
        _mainTabelView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        

    }

    return _mainTabelView;


}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
     [self setCommonProperties];
     //设置UI
     [self setupUI];
     
     // 添加导航
     [self addNaVigationView];
    
    
    //发送网络请求
    [self sendNetworkRequest];
    
    [self setupNavigateshuju];
    
    
    
    
    
}




// 设置导航数据
- (void)setupNavigateshuju {
    
    self.zhanghaoLabel.text = [NSString stringWithFormat:@"%@",[HWLocallyStoredData getUsername]];
    self.qudaoLabel.text = [NSString stringWithFormat:@"%@",@"用户名"];
    [self.icoIamgeView  sd_setImageWithURL:[NSURL URLWithString:[HWLocallyStoredData getHead]] placeholderImage:nil];
    
    
    
}

- (void)setCommonProperties  {
    
    self.view.backgroundColor =  beijingyanSe;
    
}

// 发送网络请求
- (void)sendNetworkRequest {
    
    
    
     
         NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
         NSString *uid =  [HWLocallyStoredData getLoginUserID];
         NSString *signStr = [NSString stringWithFormat:@"%@%@%@",uid,tStr,@"adesxloksy13s98adv"];
         NSString  *signStrMd5  =   [WMUtil md5HexDigest:signStr];
           
        
        
        
          NSDictionary *parametes = @{      @"ios" : @"1",
                                       @"ct" : @"app",
                                        @"ac" : @"cps",
                                        @"t" : tStr,
                                        @"uid":uid,
                                        @"sign" :signStrMd5
                                        };
        
        
            [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
                  
                 

                  NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
                  if ([codeStr intValue] == 1) {
                 
                        NSLog(@"%@",response);
                      
                      self.dataDic = response[@"data"];
                      self.zeongjingerLabel.text = [NSString stringWithFormat:@"%@",self.dataDic[@"money"]];
                      self.fengxiangyongjingLabel.text = [NSString stringWithFormat:@"%@",self.dataDic[@"shareMoney"]];
                      self.yitixianjingLabel.text = [NSString stringWithFormat:@"%@",self.dataDic[@"withdraw"]];
                    //  self.hanshuInter
                      NSString *show = [NSString stringWithFormat:@"%@",self.dataDic[@"show"]];
                      self.hanshuInter = [show integerValue];
                      
                      
                      if (self.hanshuInter == 2) {
                          
                          self.shengqingjiesuanBut.hidden = YES;
                          
                      } else {
                            self.shengqingjiesuanBut.hidden = NO;
                          
                      }
                      
                      
                      
                      [self.mainTabelView reloadData];
                      
                       
                 

                       
                  } else {
                      
                      [MBProgressHUD showError:response[@"message"]];
                                    
                
                      
                  }
                  
             
                  
              } failure:^(NSString *error) {
                 // [SVProgressHUD dismiss];
                  
                  [MBProgressHUD showSuccess:@"登录失败"];
                  [MBProgressHUD hideHUD];
                  
                  
                  
                  
              }];
        
    
}





// 添加导航
-(void)addNaVigationView  {
    // 这个是 66
     self.navigationTopView  = [[UIView alloc]initWithFrame:CGRectMake(0, 0 , SCREEN_WIDTH, 44+StatusBarHeight+20)];
     self.navigationTopView .backgroundColor = [UIColor whiteColor];
      [self.view addSubview:  self.navigationTopView ];
    
      self.zhanghaoLabel  = [[UILabel alloc]initWithFrame:CGRectMake(20, StatusBarHeight+15, 300, 25)];
      self.zhanghaoLabel.text = @"63527373";
      self.zhanghaoLabel.textAlignment =  NSTextAlignmentLeft;
      self.zhanghaoLabel.textColor = RGB(68, 68, 68);
      self.zhanghaoLabel.font = [UIFont systemFontOfSize:18];
    
     
      [self.navigationTopView  addSubview:self.zhanghaoLabel];
    
    
       self.qudaoLabel  = [[UILabel alloc]initWithFrame:CGRectMake(20, self.zhanghaoLabel.bottom, 300, 12)];
       self.qudaoLabel.text = @"63527373";
        self.qudaoLabel.textAlignment =  NSTextAlignmentLeft;
       self.qudaoLabel.textColor = RGB(193, 193, 203);
       self.qudaoLabel.font = [UIFont systemFontOfSize:10];
       [self.navigationTopView  addSubview:self.qudaoLabel];
    
    
      self.icoIamgeView = [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH - 36-20, StatusBarHeight+15, 36, 36)];
      self.icoIamgeView.backgroundColor = [UIColor redColor];
      self.icoIamgeView.layer.cornerRadius = 12;
      self.icoIamgeView.layer.masksToBounds  = YES;
      [self.navigationTopView  addSubview:self.icoIamgeView];
    
    
}





//设置UI
- (void)setupUI {
    
    [self.view addSubview:self.mainTabelView];
    self.hederView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH,20+116+69+20+71+20)];
    self.hederView.backgroundColor =beijingyanSe;
    self.mainTabelView.tableHeaderView = self.hederView;
    self.mainTabelView.tableFooterView = [[UIView alloc]init];
    
    //添加headerView
    [self addheadeView];
    
    //推广公告UIview
    [self addtuiguanAndfenchengView];
    
    
    
    
    
    
    
}


//添加headerView
- (void)addheadeView {
    
    self.kapianView = [[UIView alloc]initWithFrame:CGRectMake(20, 20, kScreenW-20*2, 116+69)];
    self.kapianView.layer.cornerRadius = 8;
    self.kapianView.layer.masksToBounds = YES;
    self.kapianView.backgroundColor = [UIColor whiteColor];
    [self.hederView addSubview:self.kapianView];
    
    UIView *lanseView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.kapianView.width, 116)];
    lanseView.backgroundColor = RGB(74, 79, 242);
    [self.kapianView   addSubview:lanseView];
     
    UILabel   *tuguanLabel  = [[UILabel alloc]initWithFrame:CGRectMake(14, 14, 200, 17)];
    tuguanLabel.text = @"总金额 (元)";
    tuguanLabel.textAlignment =  NSTextAlignmentLeft;
    tuguanLabel.textColor = RGB(255, 255, 255);
    tuguanLabel.font = [UIFont systemFontOfSize:12];
    [lanseView  addSubview:tuguanLabel];
    
    
    UIButton *wangjibutton = [UIButton buttonWithType:UIButtonTypeCustom];
      wangjibutton.frame = CGRectMake(self.kapianView.width-16-62,12,62, 22);
      [wangjibutton setTitle:@"申请结算" forState:UIControlStateNormal];
      [wangjibutton setTitleColor:RGB(255, 255, 255) forState:UIControlStateNormal];
      wangjibutton.titleLabel.font = [UIFont systemFontOfSize:12];
      [wangjibutton addTarget:self action:@selector(jiesuanbuttonTap) forControlEvents:UIControlEventTouchUpInside];
      wangjibutton.layer.cornerRadius = 11;
      wangjibutton.layer.masksToBounds  = YES;
    wangjibutton.layer.borderWidth = 1;
    wangjibutton.layer.borderColor = RGB(255, 255, 255).CGColor;
    self.shengqingjiesuanBut = wangjibutton;
     // wangjibutton.backgroundColor = [UIColor redColor];
    [lanseView addSubview:wangjibutton];
    
    

    
  //   self.zeongjingerLabel
    
    
    
      self.zeongjingerLabel  = [[UILabel alloc]initWithFrame:CGRectMake(10, 45, self.kapianView.width-20, 50)];
      self.zeongjingerLabel .text = @"2400.00";
      self.zeongjingerLabel.textAlignment =  NSTextAlignmentCenter;
      self.zeongjingerLabel.textColor = RGB(255, 255, 255);
      self.zeongjingerLabel.font = [UIFont systemFontOfSize:36];
      [lanseView  addSubview:  self.zeongjingerLabel];
    
    
    UIView *fenxianView = [[UIView alloc]initWithFrame:CGRectMake(0,lanseView.bottom, self.kapianView.width, 69)];
    fenxianView.backgroundColor = [UIColor whiteColor];
    [self.kapianView addSubview:fenxianView];
    
    UILabel *fenxiangLabel  = [[UILabel alloc]initWithFrame:CGRectMake(0, 14, self.kapianView.width/2, 17)];
    fenxiangLabel.text = @"可提现佣金";
    fenxiangLabel.textAlignment =  NSTextAlignmentCenter;
    fenxiangLabel.textColor = RGB(193 ,193, 203);
    fenxiangLabel.font = [UIFont systemFontOfSize:12];
    [fenxianView  addSubview:  fenxiangLabel];
    
    
    self.fengxiangyongjingLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, fenxiangLabel.bottom+2, self.kapianView.width/2, 22)];
    self.fengxiangyongjingLabel .text = @"66.00";
    self.fengxiangyongjingLabel .textAlignment =  NSTextAlignmentCenter;
    self.fengxiangyongjingLabel .textColor = RGB(68 ,68, 68);
    self.fengxiangyongjingLabel .font = [UIFont systemFontOfSize:16];
    [fenxianView  addSubview:  self.fengxiangyongjingLabel];
    
    
      UILabel *tixianfenxiangLabel  = [[UILabel alloc]initWithFrame:CGRectMake(self.kapianView.width/2, 14, self.kapianView.width/2, 17)];
      tixianfenxiangLabel.text = @"已提现佣金";
      tixianfenxiangLabel.textAlignment =  NSTextAlignmentCenter;
      tixianfenxiangLabel.textColor = RGB(193 ,193, 203);
      tixianfenxiangLabel.font = [UIFont systemFontOfSize:12];
      [fenxianView  addSubview:  tixianfenxiangLabel];
    
    //  self.yitixianjingLabel
    
      self.yitixianjingLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.kapianView.width/2, fenxiangLabel.bottom+2, self.kapianView.width/2, 22)];
       self.yitixianjingLabel .text = @"20.00";
       self.yitixianjingLabel .textAlignment =  NSTextAlignmentCenter;
       self.yitixianjingLabel .textColor = RGB(68 ,68, 68);
       self.yitixianjingLabel .font = [UIFont systemFontOfSize:16];
       [fenxianView  addSubview:  self.yitixianjingLabel];
       
      UIView *shuxangView = [[UIView alloc]initWithFrame:CGRectMake(self.kapianView.width/2, 20, 1, 30)];
      shuxangView.backgroundColor = RGB(193, 193, 203);
      [fenxianView addSubview:shuxangView];
    
    
    
    
    
    
}



// 推广Uiview
- (void)addtuiguanAndfenchengView {
    
    self.tuiguanView = [[UIView alloc]initWithFrame:CGRectMake(0, self.kapianView.bottom+20, kScreenW, 71)];
    self.tuiguanView.backgroundColor = [UIColor whiteColor];
    [self.hederView addSubview:self.tuiguanView];
    
     NSArray *fanlilistArr = @[@"推广公告",@"推广二维码",@"分成明细"];
      NSArray   *paihangbangcoArr = @[@"tiuguangong_ico",@"erwima_ico",@"fengchenxiangqing_ico"];
      CGFloat hengjianjuLiin = (kScreenW-  65*3)/4;
     
      for (int i = 0 ; i < fanlilistArr.count; i++) {
      
          int row = i/3;
          int line = i%3;
//          int shulian = (line*2+2) -1;
          UIView *redview = [[UIView alloc]initWithFrame:CGRectMake(hengjianjuLiin+hengjianjuLiin*line+line*65, 13+row*47, 65,47 )];
          redview.tag = i;
          redview.backgroundColor = [UIColor whiteColor];
                                                      
          [self.tuiguanView  addSubview:redview];
          
          
          UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imviewTaKaifu:)];
          [redview addGestureRecognizer:tap1];
          
          
          UIImageView *imview = [[UIImageView alloc]initWithFrame:CGRectMake(20, 0,  24, 24)];
          imview.image = [UIImage imageNamed:paihangbangcoArr[i]];
          imview.userInteractionEnabled = YES;
          imview.tag = i;
          [redview addSubview:imview];
          
        //  [self.imageMuArr addObject:imview];
          
      
          
         // UILabel
          UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 30, 65, 17)];
          nameLabel.text = fanlilistArr[i];
          nameLabel.textAlignment = NSTextAlignmentCenter;
          nameLabel.font = [UIFont systemFontOfSize:12];
          nameLabel.textColor = RGB(68, 68, 68);
          [redview addSubview:nameLabel];
       //   [self.labelMuArr addObject:nameLabel];
          
      
      }
    
    
    
    
}




- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

      return  self.hanshuInter;
    
   //   return  4;
    
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellID = @"cellID";

//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
//
//    if (!cell) {
//        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
//    }
//
//
//    return  cell;
    
    HWPromoteTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    
    if (!cell) {
        cell = [[NSBundle mainBundle]loadNibNamed:@"HWPromoteTableViewCell" owner:nil options:nil].lastObject;
        
        
        
    }
    
    
    cell.selectionStyle =  UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor whiteColor];
    
    
    
    if (indexPath.row == 0) {
        
        cell.icoIamgeView.image = [UIImage imageNamed:@"leijishouru"];
     cell.mingchengLabel.text = @"累计赚取";
     //    cell.mingchengLabel.text = [NSString stringWithFormat:@"%@",self.dataDic[@""accumulate]];
     //   cell.righetLabel.text = @"￥32552";
        cell.righetLabel.text  = [NSString stringWithFormat:@"%@",self.dataDic[@"accumulate"]];
        cell.righetLabel.hidden = NO;
        
        cell.righrimag.hidden = YES;
        cell.miahsulabel.hidden = YES;
        
        
        
        
        
         
    } else if ( indexPath.row == 1) {
        
        cell.icoIamgeView.image = [UIImage imageNamed:@"tuiguan_ico"];
        cell.mingchengLabel.text = @"推广用户";
        cell.righetLabel.text = @"322人";
        cell.righetLabel.text = [NSString stringWithFormat:@"%@",self.dataDic[@"popularizeNumber"]];
        cell.righetLabel.hidden = NO;
        cell.righrimag.hidden = YES;
        cell.miahsulabel.hidden = YES;
        
        
    }else if ( indexPath.row == 2) {
        
        cell.icoIamgeView.image = [UIImage imageNamed:@"zhekou_ico"];
        cell.mingchengLabel.text = @"折扣代充";
         cell.righetLabel.hidden = YES;
        cell.righrimag.hidden = NO;
        cell.miahsulabel.hidden = YES;
        
        
    } else {
        
        cell.icoIamgeView.image = [UIImage imageNamed:@"tuiguantyouxi_ico"];
        cell.mingchengLabel.text = @"推广游戏";
        cell.righetLabel.hidden = YES;
        cell.righetLabel.text = [NSString stringWithFormat:@"%@",self.dataDic[@"gameDescription"]];
        
        cell.righrimag.hidden = NO;
        cell.miahsulabel.hidden = NO;
        
        
    }
    

    return  cell;
    
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    return  44;

    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath  {
    
    
    
    if (indexPath.row == 0 ) {
        
        
        
    } else if (indexPath.row == 1) {
        
        
    } else if (indexPath.row == 2) {
        
        HWDiscountDaiChunViewController *discountDaiChun = [[HWDiscountDaiChunViewController alloc]init];
        [self.navigationController pushViewController:discountDaiChun animated:YES];
        
        
    } else {
        
        HWPromoteGameViewController *promoteGame = [[HWPromoteGameViewController alloc]init];
        [self.navigationController pushViewController:promoteGame animated:YES];
        
        
        
        
    }
    
    
    
    
}






#pragma mark --------- 点击申请结算

- (void)jiesuanbuttonTap {
    
    
    DLog(@"结算");
    
    HWApplyjiesuanViewController  *Applyjiesuan = [[HWApplyjiesuanViewController alloc]init];
    Applyjiesuan.jisuanStr = [NSString stringWithFormat:@"%@",self.dataDic[@"shareMoney"]];
    
    [self.navigationController pushViewController:Applyjiesuan animated:YES];
    
    
}



#pragma mark --------- 点击Image
- (void)imviewTaKaifu:(UITapGestureRecognizer *)sender {
    
    NSLog(@"koojpdjopv");
    
    
    UIView  *view = sender.view;
    NSInteger tag =  view.tag;
    
    if (tag == 0) {
        
        HWPromotionAnnouncementViewController  *PromotionAnnouncement = [[HWPromotionAnnouncementViewController alloc]init];
        [self.navigationController pushViewController:PromotionAnnouncement animated:YES];
        
        
        
        
        
    } else if (tag == 1) {
        
        
//        if (self.hanshuInter == 2) {
//            
//            [MBProgressHUD showError:@"此功能暂未开放"];
//    
//            return;
//        }
        
        HWPromotionQrCodeViewController *PromotionQr = [[HWPromotionQrCodeViewController alloc]init];
        [self.navigationController pushViewController:PromotionQr animated:YES];
        
        
    } else {
        
        HWDividedIntoViewController *divided = [[HWDividedIntoViewController alloc]init];
        [self.navigationController pushViewController:divided animated:YES];
        
        
    }
    
    
}


@end
